package com.lianniu.mgr.product.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.lianniu.mgr.base.BaseResult;
import com.lianniu.mgr.baseService.serialNo.model.SerialNoVo;
import com.lianniu.mgr.baseService.staff.model.StaffVo;
import com.lianniu.mgr.pagination.PaginationInfo;
import com.lianniu.mgr.product.model.ProductSpecValueVo;
import com.lianniu.mgr.product.model.ProductSpecVo;
import com.lianniu.mgr.product.service.IProductSpecService;
import com.lianniu.mgr.projectMgr.projectInfo.model.ProjectStaffVo;
import com.lianniu.mgr.util.StringUtil;
@Controller
@RequestMapping("/rest/productSpec")
public class ProductSpecRestController {
	private final static Logger logger = Logger.getLogger(ProductSpecRestController.class);
	  
	
	  @Autowired
	  private IProductSpecService productSpecService;
	  
	  @RequestMapping(value="/ProductSpecList",method={RequestMethod.GET,RequestMethod.POST})
	  public @ResponseBody BaseResult ProductSpecList(HttpServletRequest request, ProductSpecVo psVo, PaginationInfo paginationInfo)
	  {
	       logger.info("ProductSpecRestController  ProductSpecList begin.");
	       try {
	           
	           logger.info("ProductSpecList begin.");          
	           BaseResult baseResult = productSpecService.selectByProductSpecForPage(psVo, paginationInfo);
	           logger.info("ProductSpecList end.");
	           logger.info("ProductSpecRestController ProductSpecList end.");
	           return baseResult;
	       } catch (Exception e) {
	       logger.error("ProductSpecRestController ProductSpecList error:" + e.getMessage());
	       logger.error("ProductSpecRestController ProductSpecList end.");
	       return BaseResult.exception(e.getMessage());
	     }
	  }
	  
	  @RequestMapping(value = {"/ProductSpecAdd"}, method = RequestMethod.POST)
	  public @ResponseBody BaseResult productspecAdd(HttpServletRequest request,ProductSpecVo psVo) {   
	      try {
	          logger.info("ProductSpecAdd begin.");
	     // 保存到数据库中
	        String[] specId = request.getParameterValues("specId");
			String cateId = request.getParameter("parentName");
			String[] specName = request.getParameterMap().get("specName");
			String[] type =request.getParameterMap().get("type");
			String[] sortNum =request.getParameterMap().get("sortNum");
			String[] isValidate = request.getParameterMap().get("isValidate");
			String[] isNecessary = request.getParameterMap().get("isNecessary");
			String[] remark = request.getParameterMap().get("remark");
			if(specId != null){
			for(int i=0;i<specName.length-specId.length;i++){
					psVo.setCateId(cateId);
					psVo.setSpecName(specName[specName.length-i-1]);
					psVo.setType(type[specName.length-i-1]);
					psVo.setIsNecessary(isNecessary[specName.length-i-1]);
					psVo.setIsValidate(isValidate[specName.length-i-1]);
					psVo.setDelFlag("1");
					psVo.setRemark(remark[specName.length-i-1]);
					psVo.setSortNum(sortNum[specName.length-i-1]);
					productSpecService.insertProductSpec(psVo);
				}		
			}else{
				for(int i=0;i<specName.length;i++){
					psVo.setCateId(cateId);
					psVo.setSpecName(specName[i]);
					psVo.setType(type[i]);
					psVo.setIsNecessary(isNecessary[i]);
					psVo.setIsValidate(isValidate[i]);
					psVo.setDelFlag("1");
					psVo.setRemark(remark[i]);
					psVo.setSortNum(sortNum[i]);
					productSpecService.insertProductSpec(psVo);
				}
			}
			BaseResult baseResult = productSpecService.selectById(psVo);
			
			logger.info("ProductSpecRestController ProductSpecAdd begin.");
			logger.info("ProductSpecRestController ProductSpecAdd:"+ JSON.toJSONString(psVo));
	          
	          logger.info("ProductSpecAdd baseResult:" + JSON.toJSONString(baseResult));
	          logger.info("ProductSpecAdd end.");
	          return baseResult;
	    } catch (Exception e) {
	      logger.error("ProductSpecAdd error:"+e.getMessage());
	      logger.info("ProductSpecRestController ProductSpecAdd end.");
	      return BaseResult.exception(e.getMessage());
	    }

	  } 
	  
	  @RequestMapping(value = {"/ProductSpecEdit"}, method = RequestMethod.POST)
	  public @ResponseBody BaseResult ProductSpecEdit(HttpServletRequest request,ProductSpecVo psVo) {
	      logger.info("ProductSpecRestController ProductSpecEdit begin.");      
	      logger.info("ProductSpecRestController ProductSpecEdit:"+ JSON.toJSONString(psVo));
	      try {
	          logger.info("ProductSpecEdit begin.");
	          BaseResult baseResult = productSpecService.updateById(psVo);
	          logger.info("ProductSpecEdit baseResult:" + JSON.toJSONString(baseResult));
	          logger.info("updateById" +psVo.getSpecId());
	          logger.info("ProductSpecEdit end.");
	          return baseResult;

	    } catch (Exception e) {
	      logger.error("ProductSpecEdit error:"+e.getMessage());
	      logger.info("ProductSpecRestController ProductSpecEdit end.");
	      return BaseResult.exception(e.getMessage());
	    }
	  }  
	  
	  
	  @RequestMapping(value="/selectProductSpecById",method={RequestMethod.GET,RequestMethod.POST})
	  public @ResponseBody BaseResult selectProductSpecById(HttpServletRequest request,ProductSpecVo curVo) {
	        logger.info("ProductSpecRestController selectProductSpecById begin.");
	        try {
	                    
	          BaseResult baseResult = productSpecService.selectById(curVo);
	          logger.info("selectDictionaryById end.");
	          logger.info("ProductSpecRestController selectProductSpecById end.");
	          return baseResult;
	        } catch (Exception e) {
	          logger.error("ProductSpecRestController selectProductSpecById error:" + e.getMessage());
	          logger.error("ProductSpecRestController selectProductSpecById end.");
	          return BaseResult.exception(e.getMessage());
	        }
	  } 
	  
	  @RequestMapping(value = {"/productSpecDel"}, method = RequestMethod.POST)
	  public @ResponseBody BaseResult productSpecDel(HttpServletRequest request) {
	      logger.info("ProductSpecRestController ProductSpecDel begin.");
	     try {          
	          logger.info("ProductSpecDel begin.");
	          BaseResult baseResult = productSpecService.deleteProductSpecBatch(request.getParameter("ids").split(","));		
	          logger.info("ProductSpecDel baseResult:" + JSON.toJSONString(baseResult));
	          logger.info("ProductSpecDel end.");
	          
	          logger.info("ProductSpecRestController ProductSpecDel end.");
	          return baseResult;

	    } catch (Exception e) {
	      logger.error("ProductSpecRestController ProductSpecDel error:" + e.getMessage());
	      logger.info("ProductSpecRestController ProductSpecDel end.");
	      return BaseResult.exception(e.getMessage());
	    }

	  }
	  
	  @RequestMapping(value="/selectParentSpec",method={RequestMethod.GET,RequestMethod.POST})
	  public @ResponseBody BaseResult selectParentCate(HttpServletRequest request) {
	        logger.info("ProductSpecRestController selectParentSpec begin.");
	        try {
	        	
	          String cateId = request.getParameter("cateId");          
	          BaseResult baseResult = productSpecService.selectParentSpec(cateId);
	          logger.info("selectParentSpec end.");
	          logger.info("ProductSpecRestController selectParentSpec end.");
	          return baseResult;
	        } catch (Exception e) {
	          logger.error("ProductSpecRestController selectParentSpec error:" + e.getMessage());
	          logger.error("ProductSpecRestController selectParentSpec end.");
	          return BaseResult.exception(e.getMessage());
	        }
	  } 
	  
	  @RequestMapping(value = {"/checkSpecName"}, method = RequestMethod.POST)
	  public @ResponseBody Boolean checkSpecName(HttpServletRequest request) {
	      Boolean flag=true;
	      String specName=  request.getParameter("specName");
	      String cateId = request.getParameter("parentId");
	      logger.info("ProductSpecRestController checkSpecName begin.");
	     try {          
	          logger.info("checkspecName begin.");
	          if("".equalsIgnoreCase(specName)||specName==null)
	          {return flag;}
	          flag = productSpecService.checkSpecName(specName);         
	          logger.info("checkCateName end.");
	          
	          logger.info("ProductSpecRestController checkSpecName end.");
	          return flag;
	    } catch (Exception e) {
	      logger.error("ProductSpecRestController checkSpecName error:" + e.getMessage());
	      logger.info("ProductSpecRestController checkSpecName end.");
	      return flag;
	    }

	  }
	  
	  
	  @RequestMapping(value = {"/checkSpecNameEdit"}, method = RequestMethod.POST)
	  public @ResponseBody Boolean checkSpecNameEdit(HttpServletRequest request) {
	      Boolean flag=true;
	      String specName=  request.getParameter("specName");
	      String specId=request.getParameter("specId");
	      logger.info("ProductSpecRestController checkSpecNameEdit begin.");
	     try {          
	          logger.info("checkTypeNameEdit begin.");
	          ProductSpecVo ps=new ProductSpecVo();	         
	          ps.setSpecId(specId);
	          
	          BaseResult baseResult =productSpecService.selectById(ps);	          
	          ProductSpecVo psVo=(ProductSpecVo) baseResult.getData();
	          if(psVo.getSpecId().equals(specId)&&psVo.getSpecName().equals(specName))
	          {return flag;}
	          if("".equalsIgnoreCase(specName)||specName==null)
	          {return flag;}
	          flag = productSpecService.checkSpecName(specName);         
	          logger.info("checkSpecNameEdit end.");
	          
	          logger.info("ProductSpecRestController checkSpecNameEdit end.");
	          return flag;
	    } catch (Exception e) {
	      logger.error("ProductSpecRestController checkSpecNameEdit error:" + e.getMessage());
	      logger.info("ProductSpecRestController checkSpecNameEdit end.");
	      return flag;
	    }

	  }
	  @RequestMapping(value="/selectByProductCateId",method={RequestMethod.GET,RequestMethod.POST})
	  public @ResponseBody BaseResult selectByProductCateId(HttpServletRequest request){
		  logger.info("ProductSpecRestController selectByProductCateId begin.");
		  try {
	        	
	          String cateId = request.getParameter("cateId");          
	          BaseResult baseResult = productSpecService.selectByProductCateId(cateId);
	          logger.info("selectByProductCateId end.");
	          logger.info("ProductSpecRestController selectByProductCateId end.");
	          return baseResult;
	        } catch (Exception e) {
	          logger.error("ProductSpecRestController selectByProductCateId error:" + e.getMessage());
	          logger.error("ProductSpecRestController selectByProductCateId end.");
	          return BaseResult.exception(e.getMessage());
	        }
	  }
	 @RequestMapping(value = {"/SpecDel"}, method = RequestMethod.POST)
	  public @ResponseBody BaseResult SpecDel(HttpServletRequest request) {
	      logger.info("ProductSpecRestController SpecDel begin.");
	     try {          
	          logger.info("SpecDel begin.");
	          BaseResult baseResult = productSpecService.deleteById(request.getParameter("specId"));
	          logger.info("ProductSpecDel baseResult:" + JSON.toJSONString(baseResult));
	          logger.info("SpecDel end.");
	          
	          logger.info("ProductSpecRestController SpecDel end.");
	          return baseResult;

	    } catch (Exception e) {
	      logger.error("ProductSpecValueRestController SpecDel error:" + e.getMessage());
	      logger.info("ProductSpecValueRestController SpecDel end.");
	      return BaseResult.exception(e.getMessage());
	    }

	  }
}
